package com.swpu.cbg.cnshop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.squareup.picasso.Picasso;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.activity.AddressListActivity;
import com.swpu.cbg.cnshop.activity.LoginActivity;
import com.swpu.cbg.cnshop.activity.MyApplication;
import com.swpu.cbg.cnshop.activity.MyFavoriteActivity;
import com.swpu.cbg.cnshop.activity.MyOrderActivity;
import com.swpu.cbg.cnshop.bean.User;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by chenboge on 16/1/30.
 */
public class MineFragment extends BaseFragment {

    private View mView;
    private LayoutInflater mInflater;

    @ViewInject(R.id.img_head)
    private CircleImageView mImageView;

    @ViewInject(R.id.txt_username)
    private TextView mUnameText;

    @ViewInject(R.id.txt_my_orders)
    private TextView mOrderText;

    @ViewInject(R.id.txt_my_favorite)
    private TextView mFavoriteText;

    @ViewInject(R.id.txt_my_address)
    private TextView mAddressext;

    @ViewInject(R.id.btn_logout)
    private Button LoginOutBtn;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mInflater = LayoutInflater.from(getActivity());
        return mInflater.inflate(R.layout.mine, container, false);
    }

    @Override
    public void init() {
        initUserData();
    }

    private void initUserData() {
        showUser();
    }

    @OnClick(value = {R.id.img_head, R.id.txt_username})
    public void LoginClick(View v) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), LoginActivity.class);
        startActivityForResult(intent, Constans.State.STATE_OK);
    }

    @OnClick(R.id.btn_logout)
    public void LogoutClick(View v) {
        MyApplication.getInstanse().clearUser();
        showUser();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        initUserData();
    }

    private void showUser() {
        User user = MyApplication.getInstanse().getUser();
        if (user != null) {
            mUnameText.setText(user.getMobi());
            Picasso.with(getActivity()).load(user.getLogo_url()).into(mImageView);
            showLoginoutButton();
        } else {
            mUnameText.setText(R.string.to_login);
            hideLoginoutButton();
            mImageView.setImageResource(R.drawable.default_head);
        }
    }


    @OnClick(R.id.txt_my_orders)
    public void myOrder(View view){
        Intent intent = new Intent();
        intent.setClass(getActivity(), MyOrderActivity.class);
        startActivity(intent,true);
    }

    @OnClick(R.id.txt_my_favorite)
    public void myFavorite(View view){

    }

    @OnClick(R.id.txt_my_address)
    public void myAddress(View view) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), AddressListActivity.class);
        startActivity(intent,true);
    }

    @OnClick(R.id.txt_my_favorite)
    public void toFavoriteActivity(View view){
        startActivity(new Intent(getActivity(), MyFavoriteActivity.class),true);
    }


    public void showLoginoutButton() {
        LoginOutBtn.setVisibility(View.VISIBLE);
    }

    public void hideLoginoutButton() {
        LoginOutBtn.setVisibility(View.GONE);
    }

}
